package coder.taiji.test.springnetty.udp;

import coder.taiji.test.springnetty.udp.dto.LogEvent;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.util.CharsetUtil;

import java.util.List;

/**
 * Listing 13.6 LogEventDecoder
 *
 * @author <a href="mailto:norman.maurer@gmail.com">Norman Maurer</a>
 */
public class LogEventDecoder extends MessageToMessageDecoder<DatagramPacket> {

	@Override
	protected void decode(ChannelHandlerContext ctx,
	                      DatagramPacket datagramPacket, List<Object> out) {
		LogEvent event = new LogEvent(datagramPacket.sender(), System.currentTimeMillis());
		try {
			ByteBuf data = datagramPacket.content();
			int idx = data.indexOf(0, data.readableBytes(),
					LogEvent.SEPARATOR);
			String filename = data.slice(0, idx)
					.toString(CharsetUtil.UTF_8);
			String logMsg = data.slice(idx + 1,
					data.readableBytes() - idx - 1).toString(CharsetUtil.UTF_8);
			event.setLogfile(filename);
			event.setMsg(logMsg);
			event.setSucc(true);
		} catch (Exception e) {
			event.setSucc(false);
		} finally {
			out.add(event);
		}

	}
}
